const mongoose = require("mongoose");
const { PORT, MONGOHOST, DATABASE } = require("./config");
// const {getPageData} = require("./test")

function mongodbStart(callback) {
  console.log("mongodb connect start ");
  // 测试连接数据
  mongoose
    .connect(`mongodb://${MONGOHOST}:${PORT}/${DATABASE}`, {
      serverSelectionTimeoutMS: 5000,
    })
    .then((data) => {
      console.log("mongodb连接成功~~~");
      callback && callback();
    })
    .catch((err) => {
      console.log("连接失败~~~");
    });
  
  // mongoose.connection.on("open", () => {
  //   console.log("mongodb 连接成功~~~");
  //   callback && callback();
  // });
  // mongoose.connection.on("error", () => {
  //   console.log("连接mogoose失败");
  // });
  // mongoose.connection.on("close", () => {
  //   console.log("连接关闭");
  // });
  // 关闭连接
  // mongoose.disconnect(()=>{})
}

module.exports = mongodbStart;
